<?php
if (!defined('ABSPATH')) {
    exit;
}

class NX_Custom_Sort_Admin {
    public function __construct() {
        add_action('admin_menu', [$this, 'add_menu']);
        add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']);
    }

    public function add_menu() {
        add_menu_page(
            __('NX Custom Sort', 'nx-custom-sort'),
            __('NX Custom Sort', 'nx-custom-sort'),
            'manage_options',
            'nx-custom-sort',
            [$this, 'settings_page'],
            'dashicons-sort',
            80
        );
    }

    public function enqueue_scripts($hook) {
        if ($hook !== 'toplevel_page_nx-custom-sort') {
            return;
        }
        wp_enqueue_script('sortablejs', NX_CUSTOM_SORT_PLUGIN_URL . 'assets/js/sortable.min.js', [], '1.15.0', true);
        wp_enqueue_script('nx-custom-sort-admin', NX_CUSTOM_SORT_PLUGIN_URL . 'assets/js/admin-sort.js', ['jquery', 'sortablejs'], '1.0.0', true);
        wp_enqueue_style('nx-custom-sort-admin', NX_CUSTOM_SORT_PLUGIN_URL . 'assets/css/admin.css', [], '1.0.0');
        wp_localize_script('nx-custom-sort-admin', 'nxCustomSort', [
            'ajax_url' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('nx-custom-sort-nonce')
        ]);
    }

    public function settings_page() {
        ?>
        <div class="wrap">
            <h1><?php _e('NX Custom Sort Settings', 'nx-custom-sort'); ?></h1>
            <form id="nx-custom-sort-form">
                <label for="rule-name"><?php _e('Rule Name', 'nx-custom-sort'); ?></label>
                <input type="text" id="rule-name" name="rule_name" required>
                <label for="post-type"><?php _e('Post Type', 'nx-custom-sort'); ?></label>
                <select id="post-type" name="post_type">
                    <option value="post">Post</option>
                    <option value="product">Product</option>
                </select>
                <label for="category-slug"><?php _e('Category Slug (optional)', 'nx-custom-sort'); ?></label>
                <input type="text" id="category-slug" name="category_slug">
                <label for="slugs"><?php _e('Slugs (comma-separated, optional)', 'nx-custom-sort'); ?></label>
                <input type="text" id="slugs" name="slugs">
                <h2><?php _e('Sort Items', 'nx-custom-sort'); ?></h2>
                <ul id="sortable-list" class="sortable"></ul>
                <button type="submit" class="button button-primary"><?php _e('Save Rule', 'nx-custom-sort'); ?></button>
            </form>
            <h2><?php _e('Create Page', 'nx-custom-sort'); ?></h2>
            <form id="create-page-form">
                <label for="rule-id"><?php _e('Select Rule', 'nx-custom-sort'); ?></label>
                <select id="rule-id" name="rule_id">
                    <?php
                    global $wpdb;
                    $rules = $wpdb->get_results("SELECT id, rule_name FROM {$wpdb->prefix}nx_custom_sort_rules");
                    foreach ($rules as $rule) {
                        echo "<option value='{$rule->id}'>{$rule->rule_name}</option>";
                    }
                    ?>
                </select>
                <button type="submit" class="button button-primary"><?php _e('Create Page', 'nx-custom-sort'); ?></button>
            </form>
            <h2><?php _e('Visit Statistics', 'nx-custom-sort'); ?></h2>
            <table class="wp-list-table widefat fixed striped">
                <thead>
                    <tr>
                        <th><?php _e('Page ID', 'nx-custom-sort'); ?></th>
                        <th><?php _e('Visit Count', 'nx-custom-sort'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $pages = get_posts(['post_type' => 'page', 'meta_key' => '_nx_custom_sort_rule_id']);
                    foreach ($pages as $page) {
                        $count = NX_Custom_Sort_DB::get_visit_count($page->ID);
                        echo "<tr><td>{$page->ID}</td><td>{$count}</td></tr>";
                    }
                    ?>
                </tbody>
            </table>
        </div>
        <?php
    }
}

?>
